package com.xueyu.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import com.xueyu.dao.BookDao;
import com.xueyu.dao.BookTypeDao;
import com.xueyu.model.Book;
import com.xueyu.model.BookType;
import com.xueyu.util.DbUtil;
import com.xueyu.util.StringUtil;

public class BookAddInternalFrame extends JInternalFrame {
	
	DbUtil dbUtil = new DbUtil();
	BookTypeDao bookTypeDao = new BookTypeDao();
	BookDao bookDao = new BookDao();
	
	private JTextField bookNameTxt;
	private JTextField authorTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField bookPriceTxt;
	
	private JComboBox bookTypeJCB;
	private BookType bookType;
	private JRadioButton manJRB;
	private JRadioButton womenJRB;
	private JTextArea bookDescTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookAddInternalFrame frame = new BookAddInternalFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookAddInternalFrame() {
		setIconifiable(true);
		setClosable(true);
		setTitle("图书添加");
		setBounds(100, 100, 422, 475);
		
		JLabel label = new JLabel("图书名称");
		
		bookNameTxt = new JTextField();
		bookNameTxt.setColumns(10);
		
		JLabel label_1 = new JLabel("作者姓名");
		
		authorTxt = new JTextField();
		authorTxt.setColumns(10);
		
		JLabel label_2 = new JLabel("作者性别");
		
		manJRB = new JRadioButton("男");
		manJRB.setSelected(true);
		buttonGroup.add(manJRB);
		
		womenJRB = new JRadioButton("女");
		buttonGroup.add(womenJRB);
		
		JLabel label_3 = new JLabel("图书价格");
		
		bookPriceTxt = new JTextField();
		bookPriceTxt.setColumns(10);
		
		JLabel label_4 = new JLabel("图书类别");
		
		bookTypeJCB = new JComboBox();
		
		JLabel label_5 = new JLabel("图书描述");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addActionPerformed(e);
			}
		});
		button.setIcon(new ImageIcon(BookAddInternalFrame.class.getResource("/image/add.png")));
		
		JButton button_1 = new JButton("重置");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetActionPerformed(e);
			}
		});
		button_1.setIcon(new ImageIcon(BookAddInternalFrame.class.getResource("/image/reset.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addContainerGap()
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label_5)
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE))
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label_4)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(bookTypeJCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE))
							.addGroup(groupLayout.createSequentialGroup()
								.addGap(8)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label_2)
										.addGap(18)
										.addComponent(manJRB)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(womenJRB)))
								.addGap(45)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label_1)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
									.addGroup(groupLayout.createSequentialGroup()
										.addComponent(label_3)
										.addPreferredGap(ComponentPlacement.UNRELATED)
										.addComponent(bookPriceTxt)))))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(button)
							.addGap(54)
							.addComponent(button_1)))
					.addContainerGap(27, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(27)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1))
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(womenJRB)
						.addComponent(manJRB)
						.addComponent(label_3)
						.addComponent(bookPriceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_2))
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_4)
						.addComponent(bookTypeJCB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(39)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(label_5)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(button)
						.addComponent(button_1))
					.addGap(40))
		);
		
		bookDescTxt = new JTextArea();
		scrollPane.setViewportView(bookDescTxt);
		getContentPane().setLayout(groupLayout);
		bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1, false));// 设置文本域边框
		this.fillJCB();
	}
	/**
	 * 图书添加事件处理
	 * @param evt
	 */
	private void addActionPerformed(ActionEvent evt) {
		String bookName = this.bookNameTxt.getText();
		String author = this.authorTxt.getText();
		String price = this.bookPriceTxt.getText();
		String bookDesc = this.bookDescTxt.getText();
		String sex = "";
		if(manJRB.isSelected()){
			sex = "男";
		}else{
			sex  = "女";
		}
		BookType bookType = (BookType) this.bookTypeJCB.getSelectedItem();
		int bookTypeId = bookType.getId();
		
		if(StringUtil.isEmpty(bookName)){
			JOptionPane.showMessageDialog(null, "图书名不能为空！");
			return;
		}
		if(StringUtil.isEmpty(author)){
			JOptionPane.showMessageDialog(null, "图书作者不能为空！");
			return;
		}
		if(StringUtil.isEmpty(price)){
			JOptionPane.showMessageDialog(null, "图书价格不能为空！");
			return;
		}else if(!StringUtil.isPrice(price)){
			JOptionPane.showMessageDialog(null, "图书价格格式错误！");
			return;
		}
		Connection con = null;
		Book book = new Book(bookName,author, sex, Float.parseFloat(price) , bookTypeId,  bookDesc);
		try {
			con = dbUtil.getCon();
			int ret = bookDao.add(con, book);
			if(ret==1){
				JOptionPane.showMessageDialog(null, "图书添加成功！");
				resetValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "图书添加失败！");
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "somthing wrong");
			}
		}
		
	}

	/**
	 * 重置事件处理
	 * @param evt
	 */
	private void resetActionPerformed(ActionEvent evt) {
		resetValue();		
	}

	/**
	 * 表单重置
	 */
	private void resetValue(){
		this.bookNameTxt.setText("");;
		this.bookPriceTxt.setText("");;
		this.authorTxt.setText("");;
		this.manJRB.setSelected(true);
		if(this.bookTypeJCB.getItemCount()>0){
			this.bookTypeJCB.setSelectedIndex(0);
		}
		this.bookDescTxt.setText("");
	}
	/**
	 * 初始化图书类别下拉框
	 */
	private void fillJCB(){
		Connection con = null;
		bookType = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs =bookTypeDao.list(con, new BookType());
			while(rs.next()){
				bookType = new BookType();
				bookType.setId(rs.getInt("id"));
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				this.bookTypeJCB.addItem(bookType);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
